<?php
class retrieve_shopping extends retrieve_base{

	private $module_display_options 	= array(
//		Array("SHOP_DISPLAY_STOCK","Display the main shop stock list"),
//		Array("SHOP_HOTSPOTS","Display the latest Hot items in the shop."),
//		Array("SHOP_SEARCH","Display the Shop search engine.")
	);
	/*************************************************************************************************************************
    *
    *************************************************************************************************************************/
	function create_client_details($parameters){
		$client_identifier = $this->check_parameters($parameters,"client_identifier",-1);
		/**
		* generate default inserts as required by the client
		*/
		$identifier = $this->getUID();
		$this->db->query("insert into shop_settings (ss_identifier, ss_charge_vat, ss_vat, ss_always_charge, ss_client) values ('$identifier', '1', '17.5', '0', $client_identifier)");
	}

	/**
	* This function is used to return the list of orders to be dealt with
	*/
	function getDigitalDesktop($parameters){
		$sql    = "SELECT shop_basket_status, count(shop_basket_client) as total_orders FROM shop_basket_container where shop_basket_status>=4  and shop_basket_client = $this->client_identifier group by shop_basket_status";
		$result = $this->db->query($sql);
//		$out="<grouped label=\"".LOCALE_SHOP_ORDERS."\">";
$out="";
		$total_orders=0;
		if ($this->db->num_rows($result)>0){
			while ($r=$this->db->fetch_array($result)){
				$out .= "<title identifier='".$r["shop_basket_status"]."' result='".$r["total_orders"]."'><![CDATA[".$this->shop_status($r["shop_basket_status"])."]]></title>";
				$total_orders+=$r["total_orders"];
			}
				$out .= "<title result='".$total_orders."'><![CDATA[".LOCALE_SHOP_TOTAL_ORDERS."]]></title>";
			$out .= "<commands><cmd label='".LOCALE_VIEW."'>SHOP_ORDER_VIEW_TYPE</cmd></commands>";
		}else{
			$out .= "<text><![CDATA[".LOCALE_SORRY_NO_ORDERS."]]></text>";
		}
//		$out .= "</grouped>";
		$date = date("Y/m/d H:i:s",mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"))-(24*3600));
		$sql    = "SELECT count(shop_basket_client) as total_lost_baskets FROM shop_basket_container where shop_basket_status<4 and shop_basket_container.shop_basket_date  < '".$date."' and shop_basket_client = $this->client_identifier";
		$result = $this->db->query($sql);
//		$out.="<grouped label=\"".LOCALE_SHOP_LOST_BASKETS."\">";
		if ($this->db->num_rows($result)>0){
			while ($r=$this->db->fetch_array($result)){
				$name=$r["total_lost_baskets"];
				if ($name==0){
					$out .= "<text><![CDATA[".LOCALE_SORRY_NO_LOST_BASKETS."]]></text>";
				}else{
					$out .= "<title identifier='-1'><![CDATA[".$name."]]></title>";
				}
			}
			$out .= "<commands><cmd label='".REMOVE_EXISTING."'>SHOP_BASKET_CLEAN_UP</cmd></commands>";
		}else{
			$out .= "<text><![CDATA[".LOCALE_SORRY_NO_LOST_BASKETS."]]></text>";
		}
//		$out .= "</grouped>";
		
		$out = "<module name=\"shop\" label=\"".MANAGEMENT_SHOP."\" display=\"my_workspace\">".$out."</module>";
		return $out;
	}
	/*************************************************************************************************************************
    * used by my_workspace , display_list_orders
    * my_workspace in now getDigitalDesktop in the file retrieve/shopping.php duplicate all changes there too 
    *************************************************************************************************************************/
	function shop_status($int){
		if ($int==1 || $int==0)
			return LOCALE_SHOP_BASKET_IN_USE;
		if ($int==2)
			return LOCALE_SHOP_BASKET_AT_CHECKOUT;
		if ($int==3)
			return LOCALE_SHOP_BASKET_REQUESTING_PAYMENT;
		if ($int==4)
			return LOCALE_SHOP_BASKET_PURCHASE_COMPLETE;
		if ($int==5)
			return LOCALE_SHOP_BASKET_PURCHASE_BEING_PROCESSED;
		if ($int==6)
			return LOCALE_SHOP_BASKET_READY_DELIVERY;
		if ($int==7)
			return LOCALE_SHOP_BASKET_PURCHASE_BEING_DELIVERED;
		if ($int==8)
			return LOCALE_SHOP_BASKET_PURCHASE_DELIVERED;
		if ($int==9)
			return LOCALE_SHOP_BASKET_PURCHASE_REJECTED;
		if ($int==10)
			return LOCALE_SHOP_BASKET_PURCHASE_OUT_OF_STOCK;
	}

	/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();
		/**
		* Table structure for table 'shop_settings'
		*/
		$fields = array(
			array("ss_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("ss_client"				,"unsigned integer"			,""			,"default '0'"),
			array("ss_charge_vat"			,"unsigned small integer"	,""			,"default '0'"),
			array("ss_vat"					,"double"					,""			,"default '0'"),
			array("ss_always_charge"		,"unsigned small integer"	,""			,"default '0'"),
			array("ss_delivery_always_same"	,"unsigned small integer"	,""			,"default '0'"),
			array("ss_can_pay_invoice"		,"unsigned small integer"	,""			,"default '0'"),
			array("ss_can_request_invoice"	,"unsigned small integer"	,""			,"default '0'"),
			array("ss_cc_accept_label"		,"varchar(255)"				,""			,"default ''"),
			array("ss_cc_deny_label"		,"varchar(255)"				,""			,"default ''"),
			array("ss_invoice_msg_label"	,"varchar(255)"				,""			,"default ''"),
			array("ss_invoice_email_label"	,"varchar(255)"				,""			,"default ''"),
			array("ss_currency"				,"varchar(3)"				,""			,"default 'GBP'"),
			array("ss_autoreduce"			,"unsigned small integer"	,""			,"default '0'")
		);
		$primary ="ss_identifier";
		$tables[count($tables)] = array("shop_settings",$fields,$primary);

		/**
		* Table structure for table 'shop_weight'
		*/
		$fields = array(
			array("swm_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("swm_client"				,"unsigned integer"			,""			,"default '0'"),
			array("swm_country"				,"integer"					,""			,"default '0'"),
			array("swm_region"				,"varchar(2)"					,""			,"default '0'"),
			array("swm_price"				,"double"					,""			,"default '0'"),
			array("swm_kg"					,"double"					,""			,"default '0'") // -1 represents above highest
		);
		$primary ="swm_identifier";
		$tables[count($tables)] = array("shop_weight_matrix",$fields,$primary);

		/**
		* Table structure for table 'shop_weight_matrix_list'
		*/
		$fields = array(
			array("sw_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("sw_client"				,"unsigned integer"			,""			,"default '0'"),
			array("sw_max_kg"				,"double"					,""			,"default '0'")
		);
		$primary ="sw_identifier";
		$tables[count($tables)] = array("shop_weight_matrix_list",$fields,$primary);
		/**
		* Table structure for table 'shop_basket_container'
		*/
		$fields = array(
			array("shop_basket_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("shop_basket_client"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_basket_shop"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_basket_session"				,"varchar(32)"				,""			,"default ''"),
			array("shop_basket_user"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_basket_bill_contact"		,"unsigned integer"			,""			,"default '0'"),
			array("shop_basket_delivery_contact"	,"unsigned integer"			,""			,"default '0'"),
			array("shop_basket_status"				,"unsigned small integer"	,""			,"default '0'"),
			array("shop_basket_processed_by"		,"unsigned integer"			,""			,"default '0'"),
			array("shop_basket_date"				,"datetime"					,""			,"default ''"),
			array("shop_basket_payment"				,"small integer"			,""			,"default '0'"),
			array("shop_basket_requires_invoice"	,"small integer"			,""			,"default '1'"),
			array("shop_basket_invoice_sent_date"	,"datetime"					,""			,"default ''")
		);
		$primary ="shop_basket_identifier";
		$tables[count($tables)] = array("shop_basket_container",$fields,$primary);
		/**
		* Table structure for table 'shop_basket_items'
		*/
		$fields = array(
			array("shop_item_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("shop_item_basket"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_item_client"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_item_shop"					,"unsigned integer"			,""			,"default '0'"),
			array("shop_item_stock_id"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_item_stock_group"			,"unsigned integer"			,""			,"default '0'"),
			array("shop_item_pickup_price"			,"double"					,""			,"default '0'"),
			array("shop_item_pickup_discount"		,"double"					,""			,"default '0'"),
			array("shop_item_quantity"				,"unsigned integer"			,""			,"default '0'"),
			array("shop_item_weight"				,"double"					,""			,"default '0'"),
			array("shop_item_title"					,"varchar(255)"				,""			,"default ''"),
			array("shop_item_description"			,"varchar(255)"				,""			,"default ''"),
			array("shop_item_vat"					,"unsigned small integer"	,""			,"default '0'")
		);
		$primary ="shop_item_identifier";
		$tables[count($tables)] = array("shop_basket_items",$fields,$primary);
		/**
		* Table structure for table 'shop_basket_status'
		*/
		$fields = array(
			array("shop_basket_status_identifier"			,"unsigned integer"	,"NOT NULL"	,"auto_increment"),
			array("shop_basket_status_label"				,"varchar(255)"		,""			,"default ''")
		);
		$primary ="shop_basket_status_identifier";
		$tables[count($tables)] = array("shop_basket_account_status",$fields,$primary);
		/**
		* Table structure for table 'shop_trigger'
		*/
		$fields = array(
			array("st_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("st_basket_item"			,"unsigned integer"			,""			,"default ''"),
			array("st_client"				,"unsigned integer"			,""			,"default '0'"),
			array("st_executed"				,"unsigned small integer"	,""			,"default '0'"),
			array("st_cmd"					,"varchar(255)"				,""			,"default ''")
		);
		$primary ="st_identifier";
		$tables[count($tables)] = array("shop_trigger",$fields,$primary);
		/**
		* Table structure for table 'shop_trigger_parameters'
		*/
		$fields = array(
			array("stp_trigger"			,"unsigned integer"			,""	,"default '0'"),
			array("stp_client"			,"unsigned integer"			,""	,"default '0'"),
			array("stp_rank"			,"unsigned integer"			,""	,"default '0'"),
			array("stp_key"				,"varchar(255)"				,""	,"default ''"),
			array("stp_value"			,"varchar(255)"				,""	,"default ''")
		);
		$primary ="";
		$tables[count($tables)] = array("shop_trigger_parameters",$fields,$primary);

		return $tables;
	}

}
?>